﻿#include <QCoreApplication>
#include"opencv.hpp"
using namespace cv;
using namespace std;

#include<iostream>
Point center;
RotatedRect center_ellipse_rect;


Mat getBigmask(Mat src){

  equalizeHist(src, src);
  threshold(src, src, 185, 255, 0);

  namedWindow("threshold",WINDOW_FREERATIO);
  imshow("threshold",src);

  Mat hline = getStructuringElement(MORPH_RECT, Size(13, 13), Point(-1, -1));
  erode(src, src, hline);
  dilate(src, src, hline);
  Mat hline11 = getStructuringElement(MORPH_RECT, Size(10, 10), Point(-1, -1));
  dilate(src, src, hline11);

  namedWindow("hline",WINDOW_FREERATIO);
  imshow("hline",src);

  Canny(src, src, 50, 200);

  namedWindow("Canny",WINDOW_FREERATIO);
  imshow("Canny",src);

  Mat hline1 = getStructuringElement(MORPH_RECT, Size(13, 13), Point(-1, -1));
  dilate(src, src, hline1);
//  erode(src, src, hline1);

  namedWindow("dilate",WINDOW_FREERATIO);
  imshow("dilate",src);




     std::vector<std::vector<cv::Point>> contours;
      std::vector<cv::Vec4i> hierarchy;
      Mat mask = Mat::zeros(src.rows,src.cols,src.type());
      Mat mask111 = Mat::zeros(src.rows,src.cols,CV_8UC3);
    //只输出最顶层轮廓
      float contours_area;
    findContours(src, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
    cv::RotatedRect ellipse_rect;
    for (int i = 0; i < contours.size(); i++)
    {

      if(contours[i].size() > 15)
      {
         ellipse_rect = fitEllipse(contours[i]);
         cv::Scalar color;
                 color = cv::Scalar(50,128,200);
         int w = ellipse_rect.size.width;
         int h = ellipse_rect.size.height;
         if(w<src.cols/3||h<src.rows/3)
           continue;
         ellipse(src, ellipse_rect, color, 2, 8);
         ellipse(mask, ellipse_rect, cv::Scalar(255,128,200), 2, 8);
         Mat mask12 = Mat::zeros(src.rows,src.cols,CV_8UC1);
         ellipse(mask12, ellipse_rect, cv::Scalar(255,128,200), 4, 8);

          ellipse(mask111, ellipse_rect, cv::Scalar(0,255,255), 4, 8);
          drawContours(mask111, contours, i, cv::Scalar(255,0,255), 1, 8, hierarchy, 0);
//          Moments  mo = moments(contours[i], false);
           contours_area = contourArea(contours[i]);

         center_ellipse_rect = ellipse_rect;
      }
    }


  float ell_area;
    floodFill(mask, cv::Point(ellipse_rect.size.width, ellipse_rect.size.height), Scalar(255, 255, 255));
    findContours(mask, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE, cv::Point());
    for (int i = 0; i < contours.size(); i++)
    {

      if(contours[i].size() > 15)
      {

           ell_area = contourArea(contours[i]);

      }
    }
     cout<< (ell_area - contours_area)/ell_area;

    namedWindow("mask111",WINDOW_FREERATIO);
    imshow("mask111",mask111);
    return mask;
}



int main(int argc, char *argv[])
{
  QCoreApplication a(argc, argv);


  cv::Mat src = cv::imread("E:/workspace/qt/DaHeng/ng/ng09.bmp");

  resize(src,src,Size(src.cols*3,src.rows*3));
  namedWindow("src",WINDOW_FREERATIO);
  imshow("src",src);


  cvtColor(src, src, COLOR_BGR2GRAY);
  Mat ori = src.clone();

  //得到大圆
  Mat mask =   getBigmask(src);
  namedWindow("mask",WINDOW_FREERATIO);
  imshow("mask",mask);

    Mat big_src = ori & mask;

  cv::waitKey(0);


  return a.exec();
}


#include<iostream>
#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv;

Mat src, dst;
void Threshold_Demo(int typeValue, void*)
{
//  cvtColor(src, dst, COLOR_BGR2GRAY);
  dst = src.clone();



//  Mat hline2 = getStructuringElement(MORPH_RECT, Size(2, typeValue), Point(-1, -1));
//  erode(dst, dst, hline2);
// dilate(dst, dst, hline2);


 threshold(dst, dst, typeValue, 255, 0);
   Mat hline1 = getStructuringElement(MORPH_RECT, Size(20, 2), Point(-1, -1));
 //  dilate(dst, dst, hline1);
   erode(dst, dst, hline1);
  namedWindow("二值化图像",WINDOW_FREERATIO);
  imshow("二值化图像", dst);
}

int main1(){
  src = imread("E:/workspace/qt/DaHeng/ng/add_dragon.jpg");
  if (src.empty())
  {
    cout << "could not load image..." << endl;
    return -1;
  }
  cvtColor(src, src, COLOR_BGR2GRAY);
//  normalize(src,src,
//  equalizeHist(src, src);
  namedWindow("原图",WINDOW_FREERATIO);
  imshow("原图", src);
//    Mat kernel = (Mat_<char>(3, 3) << -1, 0, 1,
//                       -2, 0, 2,
//                       -1, 0, 1);


//      filter2D(src, src,-1, kernel);
  int a = 1;
  Threshold_Demo(10, 0);

  createTrackbar("Type Value", "二值化图像", &a, 255, Threshold_Demo);

  waitKey(0);
  return 0;
}
